package com.dhcc.rabbitmq.topicexchange.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class TopicRabbitConfig {

	public final static String message = "q_topic_message";
	public final static String messages = "q_topic_messages";
    //交换机名字
    public final static  String TOPIC_EXCHANGE_NAME = "mybootexchange";
    //key等于topic.key1的，后面将配置为只被queue1接收
    final static  String TOPIC_KEY_ROUTE1 = "topic.message";
    //key匹配topic.#的都被接收进queue2
    final static  String TOPIC_KEY_ROUTE2 = "topic.#";
    @Bean
    public Queue queueMessage() {
        return new Queue(TopicRabbitConfig.message);
    }

    @Bean
    public Queue queueMessages() {
        return new Queue(TopicRabbitConfig.messages);
    }

    /**
     * 声明一个Topic类型的交换机
     * @return
     */
    @Bean
    TopicExchange exchange() {
        return new TopicExchange(TopicRabbitConfig.TOPIC_EXCHANGE_NAME);
    }

    /**
     * 绑定Q到交换机,并且指定routingKey
     * @param queueMessage
     * @param exchange
     * @return
     */
    //queue1--交换机--匹配规则1
    @Bean
    Binding bindingExchangeMessage(Queue queueMessage, TopicExchange exchange) {
        return BindingBuilder.bind(queueMessage).to(exchange).with(TopicRabbitConfig.TOPIC_KEY_ROUTE1);
    }
    //queue2--交换机--匹配规则2
    @Bean
    Binding bindingExchangeMessages(Queue queueMessages, TopicExchange exchange) {
        return BindingBuilder.bind(queueMessages).to(exchange).with(TopicRabbitConfig.TOPIC_KEY_ROUTE2);
    }
    
//    @Bean
//    public Binding fanoutBinding1(){
//        return BindingBuilder.bind(queueMessage()).to(exchange()).with(TopicRabbitConfig.TOPIC_KEY_ROUTE1);
//    }
//    @Bean
//    public Binding fanoutBinding2(){
//        return BindingBuilder.bind(queueMessages()).to(exchange()).with(TopicRabbitConfig.TOPIC_KEY_ROUTE2);
//    }
}
